package com.yupi.yupicturebackend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yupi.yupicturebackend.model.entity.AIStyleTransfer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * AI风格转换Mapper
 *
 * @author yupi
 */
@Mapper
public interface AIStyleTransferMapper extends BaseMapper<AIStyleTransfer> {

    /**
     * 根据用户ID查询风格转换记录
     *
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 转换记录列表
     */
    @Select("SELECT * FROM ai_style_transfer WHERE userId = #{userId} AND isDelete = 0 ORDER BY createTime DESC LIMIT #{limit}")
    List<AIStyleTransfer> selectByUserId(@Param("userId") Long userId, @Param("limit") Integer limit);

    /**
     * 根据风格类型查询转换记录
     *
     * @param styleType 风格类型
     * @param limit 限制数量
     * @return 转换记录列表
     */
    @Select("SELECT * FROM ai_style_transfer WHERE styleType = #{styleType} AND status = 'completed' AND isDelete = 0 ORDER BY qualityScore DESC LIMIT #{limit}")
    List<AIStyleTransfer> selectByStyleType(@Param("styleType") String styleType, @Param("limit") Integer limit);

    /**
     * 查询热门风格
     *
     * @param limit 限制数量
     * @return 热门风格列表
     */
    @Select("SELECT styleName, COUNT(*) as count FROM ai_style_transfer WHERE isDelete = 0 AND status = 'completed' GROUP BY styleName ORDER BY count DESC LIMIT #{limit}")
    List<java.util.Map<String, Object>> selectPopularStyles(@Param("limit") Integer limit);

    /**
     * 统计用户风格转换次数
     *
     * @param userId 用户ID
     * @return 转换次数
     */
    @Select("SELECT COUNT(*) FROM ai_style_transfer WHERE userId = #{userId} AND isDelete = 0")
    Long countByUserId(@Param("userId") Long userId);

    /**
     * 查询相似风格转换
     *
     * @param styleName 风格名称
     * @param strength 风格强度
     * @param limit 限制数量
     * @return 相似转换列表
     */
    @Select("SELECT * FROM ai_style_transfer WHERE styleName = #{styleName} AND ABS(strength - #{strength}) < 0.2 AND status = 'completed' AND isDelete = 0 ORDER BY qualityScore DESC LIMIT #{limit}")
    List<AIStyleTransfer> selectSimilarTransfers(@Param("styleName") String styleName, @Param("strength") Double strength, @Param("limit") Integer limit);
}
